package com.cnswhy.cloud.server.merchant.controller.shop;

import com.alipay.api.domain.Shop;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.core.utils.TreeUtil;
import com.cnswhy.cloud.server.core.dto.ShopClassifyPageDTO;
import com.cnswhy.cloud.server.core.dto.ShopClassifySaveDTO;
import com.cnswhy.cloud.server.core.dto.ShopClassifyUpdateDTO;
import com.cnswhy.cloud.server.core.entity.ShopClassify;
import com.cnswhy.cloud.server.core.entity.ShopClassifySpec;
import com.cnswhy.cloud.server.core.service.ShopClassifyService;
import com.cnswhy.cloud.server.core.service.ShopClassifySpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import com.github.zuihou.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 商品分类
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-12
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/shopClassify")
@Api(value = "ShopClassify", tags = "商品分类")
//@PreAuth(replace = "shopClassify:")
public class ShopClassifyController extends SuperController<ShopClassifyService, Long, ShopClassify, ShopClassifyPageDTO, ShopClassifySaveDTO, ShopClassifyUpdateDTO> {
    @Autowired
    ShopClassifySpecService shopClassifySpecService;
    @Override
    public R<ShopClassify> handlerSave(ShopClassifySaveDTO model) {
        model.setClassifyName(model.getLabel());
        model.setSortOrder(model.getSortValue());
        return R.successDef();
    }

    @Override
    public R<ShopClassify> handlerAfterSave(ShopClassifySaveDTO shopClassifySaveDTO, ShopClassify model) {
        shopClassifySpecService.remove(Wraps.<ShopClassifySpec>lbQ().eq(ShopClassifySpec::getClassifyId,model.getId()));
        for(Long id : shopClassifySaveDTO.getSpecList()){
            ShopClassifySpec shopClassifySpec = new ShopClassifySpec();
            shopClassifySpec.setClassifyId(model.getId());
            shopClassifySpec.setSpecId(id);
            shopClassifySpecService.save(shopClassifySpec);
        }
        return R.successDef();
    }

    @Override
    public R<ShopClassify> handlerUpdate(ShopClassifyUpdateDTO model) {
        model.setClassifyName(model.getLabel());
        model.setSortOrder(model.getSortValue());
        return R.successDef();
    }

    @Override
    public R<ShopClassify> handlerAfterUpdate(ShopClassifyUpdateDTO shopClassifyUpdateDTO, ShopClassify shopClassify) {
        shopClassifySpecService.remove(Wraps.<ShopClassifySpec>lbQ().eq(ShopClassifySpec::getClassifyId,shopClassify.getId()));
        for(Long id : shopClassifyUpdateDTO.getSpecList()){
            ShopClassifySpec shopClassifySpec = new ShopClassifySpec();
            shopClassifySpec.setClassifyId(shopClassify.getId());
            shopClassifySpec.setSpecId(id);
            shopClassifySpecService.save(shopClassifySpec);
        }
        return R.successDef();
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ShopClassify> shopClassifyList = list.stream().map((map) -> {
            ShopClassify shopClassify = ShopClassify.builder().build();
            return shopClassify;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(shopClassifyList));
    }
    @ApiOperation(value = "查询商品分类", notes = "查询商品分类")
    @GetMapping("/tree")
    //@SysLog("查询系统所有的菜单")
    public R<List<ShopClassify>> allTree() {
        List<ShopClassify> list = baseService.list(Wraps.<ShopClassify>lbQ().orderByAsc(ShopClassify::getSortValue));
        /*for(ShopClassify shopClassify : list){
            shopClassify.setShopClassifySpecList(shopClassifySpecService.list(Wraps.<ShopClassifySpec>lbQ().eq(ShopClassifySpec::getClassifyId,shopClassify.getId())));
        }*/
        return success(TreeUtil.buildTree(list));
    }
}
